﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Moonwish.Silverlight.OsAvatar.Controls
{
    /// <summary>
    /// 水印文本框控件。
    /// by zhlin 2012-01-07
    /// </summary>
    public partial class WatermarkedTextbox : UserControl
    {
        public event TextChangedEventHandler TextChanged;
        private bool _isFocused;

        public WatermarkedTextbox()
        {
            InitializeComponent();
        }

        private static void TextChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as WatermarkedTextbox;
            if (control != null)
            {
                string str = (e.NewValue == null) ? string.Empty : ((string)e.NewValue);
                if (control.TextEntry != null && control.TextEntry.Text != str)
                {
                    control.TextEntry.Text = str;
                }
                if (!string.IsNullOrEmpty(str))
                {
                    VisualStateManager.GoToState(control, "WatermarkHidden", true);
                    VisualStateManager.GoToState(control, "ButtonVisible", true);
                }
            }
        }

        private void ClearFilterButtonClick(object sender, RoutedEventArgs e)
        {
            TextEntry.Text = string.Empty;
        }

        private static void TextBoxStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textbox = d as WatermarkedTextbox;
            if (textbox != null)
            {
                textbox.TextEntry.Style = (Style)e.NewValue;
            }
        }

        private void TextEntryGotFocus(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "WatermarkHidden", false);
            _isFocused = true;
        }

        private void TextEntryLostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TextEntry.Text))
            {
                VisualStateManager.GoToState(this, "WatermarkVisible", true);
            }
            _isFocused = false;
        }

        private void TextEntryTextChanged(object sender, TextChangedEventArgs e)
        {
            Text = TextEntry.Text;
            if (string.IsNullOrEmpty(TextEntry.Text))
            {
                VisualStateManager.GoToState(this, "ButtonHidden", true);
                if (!_isFocused)
                {
                    VisualStateManager.GoToState(this, "WatermarkVisible", true);
                }
            }
            else
            {
                VisualStateManager.GoToState(this, "ButtonVisible", true);
                VisualStateManager.GoToState(this, "WatermarkHidden", true);
            }
            ICommand textChangedCommand = TextChangedCommand;
            if (textChangedCommand != null)
            {
                textChangedCommand.Execute(TextChangedCommandParameter);
            }
            if (TextChanged != null)
            {
                TextChanged(sender, e);
            }
        }

        private static void WatermarkChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var textbox = d as WatermarkedTextbox;
            if (textbox != null)
            {
                string str = (e.NewValue == null) ? string.Empty : ((string)e.NewValue);
                Debug.Assert(textbox.WatermarkText != null, "textbox.WatermarkText != null");
                if (textbox.WatermarkText.Text != null && textbox.WatermarkText.Text != str)
                {
                    textbox.WatermarkText.Text = str;
                }
            }
        }

        // Properties
        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(WatermarkedTextbox), new PropertyMetadata(TextChangedHandler));


        public Style TextBoxStyle
        {
            get
            {
                return (Style)GetValue(TextBoxStyleProperty);
            }
            set
            {
                SetValue(TextBoxStyleProperty, value);
            }
        }
        public static readonly DependencyProperty TextBoxStyleProperty = DependencyProperty.Register("TextBoxStyle", typeof(Style), typeof(WatermarkedTextbox), new PropertyMetadata(TextBoxStyleChanged));


        public ICommand TextChangedCommand
        {
            get
            {
                return (ICommand)GetValue(TextChangedCommandProperty);
            }
            set
            {
                SetValue(TextChangedCommandProperty, value);
            }
        }

        public static readonly DependencyProperty TextChangedCommandProperty = DependencyProperty.Register("TextChangedCommand", typeof(ICommand), typeof(WatermarkedTextbox), null);


        public object TextChangedCommandParameter
        {
            get
            {
                return GetValue(TextChangedCommandParameterProperty);
            }
            set
            {
                SetValue(TextChangedCommandParameterProperty, value);
            }
        }
        public static readonly DependencyProperty TextChangedCommandParameterProperty = DependencyProperty.Register("TextChangedCommandParameter", typeof(object), typeof(WatermarkedTextbox), null);

        public string Watermark
        {
            get
            {
                return (string)GetValue(WatermarkProperty);
            }
            set
            {
                SetValue(WatermarkProperty, value);
            }
        }
        public static readonly DependencyProperty WatermarkProperty = DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkedTextbox), new PropertyMetadata("Enter text here", WatermarkChangedHandler));
    }
}
